#include "RCRectangle.h"

RCRectangle::RCRectangle(void)
{

}

RCRectangle::RCRectangle(XMFLOAT4  dimensions)
{
	X = dimensions.x;
	Y = dimensions.y;
	Width = dimensions.z;
	Height = dimensions.w;
}

RCRectangle::~RCRectangle(void)
{

}

void RCRectangle::Intersect(RCRectangle rectangle1, RCRectangle rectangle2,OUT RCRectangle rectangleOut)
{
	if(rectangle1.Intersects(rectangle2))
	{
		float x,y,w,h = 0;

		if(rectangle1.X >= rectangle2.X)
			x = rectangle1.X;
		else
			x = rectangle2.X;

		if(rectangle1.Y >= rectangle2.Y)
			y = rectangle1.Y;
		else
			y = rectangle2.Y;

		if(rectangle1.X + rectangle1.Width <= rectangle2.X + rectangle2.Width)
			w = (rectangle1.X + rectangle1.Width) - x;
		else
			w = (rectangle2.X + rectangle2.Width) - x;

		if(rectangle1.Y + rectangle1.Height <= rectangle2.Y + rectangle2.Height)
			h = (rectangle1.Y + rectangle1.Height) - y;
		else
			h = (rectangle2.Y + rectangle2.Height) - y;

		rectangleOut = RCRectangle(XMFLOAT4(x,y,w,h));
	}
}
RCRectangle RCRectangle::Intersect(RCRectangle rectangle1, RCRectangle rectangle2)
{
	RCRectangle retVal(XMFLOAT4(0,0,0,0));

	if(rectangle1.Intersects(rectangle2))
	{
		float x,y,w,h = 0;

		if(rectangle1.X >= rectangle2.X)
			x = rectangle1.X;
		else
			x = rectangle2.X;

		if(rectangle1.Y >= rectangle2.Y)
			y = rectangle1.Y;
		else
			y = rectangle2.Y;

		if(rectangle1.X + rectangle1.Width <= rectangle2.X + rectangle2.Width)
			w = (rectangle1.X + rectangle1.Width) - x;
		else
			w = (rectangle2.X + rectangle2.Width) - x;

		if(rectangle1.Y + rectangle1.Height <= rectangle2.Y + rectangle2.Height)
			h = (rectangle1.Y + rectangle1.Height) - y;
		else
			h = (rectangle2.Y + rectangle2.Height) - y;

		retVal = RCRectangle(XMFLOAT4(x,y,w,h));
	}

	return retVal;
}

bool RCRectangle::Intersects(RCRectangle rectangle)
{
	bool retVal = true;

	// AA Check.
	if(
		X > rectangle.X + rectangle.Width ||
		Y > rectangle.Y + rectangle.Height || 
		X + Width < rectangle.X ||
		Y + Height < rectangle.Y)
	{
		// Can't possibly have overlap
		retVal = false;
	}
	else
	{
		retVal = true;
	}


	return retVal;
}

bool RCRectangle::Contains(RCRectangle rectangle)
{
	bool retVal = false;

	if(X <= rectangle.X && X + Width >= rectangle.X + rectangle.Width &&
		Y <= rectangle.Y && Y + Height >= rectangle.Y + rectangle.Height)
		retVal = true;

	return retVal;
}

RCRectangle RCRectangle::Union(RCRectangle rectangle1,RCRectangle rectangle2)
{
	float x = 0 ,y = 0,w = 0,h = 0;

	if(rectangle1.X <= rectangle2.X)
		x = rectangle1.X;
	else
		x = rectangle2.X;

	if(rectangle1.Y <= rectangle2.Y)
		y = rectangle1.Y;
	else
		y = rectangle2.Y;

	if(rectangle1.X + rectangle1.Width >= rectangle2.X + rectangle2.Width)
		w = (rectangle1.X + rectangle1.Width) - x;
	else
		w = (rectangle2.X + rectangle2.Width) - x;

	if(rectangle1.Y + rectangle1.Height >= rectangle2.Y + rectangle2.Height)
		h = (rectangle1.Y + rectangle1.Height) - y;
	else
		h = (rectangle2.Y + rectangle2.Height) - y;

	return RCRectangle(XMFLOAT4(x,y,w,h));
}

void RCRectangle::Union(RCRectangle rectangle1,RCRectangle rectangle2, OUT RCRectangle rectangle3)
{
	float x = 0 ,y = 0,w = 0,h = 0;

	if(rectangle1.X <= rectangle2.X)
		x = rectangle1.X;
	else
		x = rectangle2.X;

	if(rectangle1.Y <= rectangle2.Y)
		y = rectangle1.Y;
	else
		y = rectangle2.Y;

	if(rectangle1.X + rectangle1.Width >= rectangle2.X + rectangle2.Width)
		w = (rectangle1.X + rectangle1.Width) - x;
	else
		w = (rectangle2.X + rectangle2.Width) - x;

	if(rectangle1.Y + rectangle1.Height >= rectangle2.Y + rectangle2.Height)
		h = (rectangle1.Y + rectangle1.Height) - y;
	else
		h = (rectangle2.Y + rectangle2.Height) - y;

	rectangle3 = RCRectangle(XMFLOAT4(x,y,w,h));
}

